﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSMAEDANP : ObservableObject
    {

        #region Campos

        private string coddan_mdn, nomdan_mdn;
        MDOgeneral mod = new MDOgeneral();

        #endregion

        #region Propiedades

        public string CODDAN_MDN
        {
            get { return coddan_mdn; }
            set { coddan_mdn = value; }
        }

        public string NOMDAN_MDN
        {
            get { return nomdan_mdn; }
            set { nomdan_mdn = value; }
        }

        private Boolean _EXISTE = false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
        }

        #endregion

        #region Metodos

        public List<CLSMAEDANP> listarDaños()
        {

            try
            {
                string query = "select coddan_mdn, nomdan_mdn from maedanp order by coddan_mdn ";
                DataTable val = mod.consulta(query);
                List<CLSMAEDANP> lisdan = new List<CLSMAEDANP>();
                foreach (DataRow r in val.Rows)
                {
                    lisdan.Add(new CLSMAEDANP
                    {
                        CODDAN_MDN = r[0].ToString(),
                        NOMDAN_MDN = r[1].ToString()
                    });
                }
                return lisdan;
            }
            catch (Exception ex) { throw (ex); }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select coddan_mdn, nomdan_mdn from maedanp where coddan_mdn ='" + CODDAN_MDN + "' order by nomdan_mdn ";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    this.CODDAN_MDN = dt.Rows[0]["CODDAN_MDN"].ToString();
                    this.NOMDAN_MDN = dt.Rows[0]["NOMDAN_MDN"].ToString();
                    
                    this.EXISTE = true;
                }
                else
                {
                    this.NOMDAN_MDN = "";
                    this.EXISTE = false;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable buscar()
        {
            try
            {
                string query = "select coddan_mdn as Codigo, nomdan_mdn as Nombre from  maedanp where ";

                if (NOMDAN_MDN != "")
                {
                    query = query + "UPPER(NOMDAN_MDN) like '%" + NOMDAN_MDN.ToUpper() + "%' and ";
                }
                query = query.Remove(query.Length - 4).Trim() + " Order by NOMDAN_MDN asc";
                return mod.consulta(query);
            }
            catch (Exception e) { throw e; }
        }

        public Boolean existe()
        {
            try
            {
                string query = "select coalesce(count(CODDAN_MDN),0) from maedanp where (CODDAN_MDN='" + CODDAN_MDN + "')";
                DataTable val = mod.consulta(query);
                if (val.Rows[0][0].ToString().Equals("0"))
                {
                    return false;
                }
                return true;
            }
            catch (Exception e) { throw e; }
        }

        #endregion

    }
}
